package com.atbun.orderservice.controller;


import com.atbun.commonutils.JwtUtils;
import com.atbun.commonutils.R;
import com.atbun.orderservice.entity.Order;
import com.atbun.orderservice.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-20
 */
@RestController
@RequestMapping("/orderservice/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param courseId
     * @param request
     * @return
     */
    @PostMapping("createOrder/{courseId}")
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request){
        String orderNo = this.orderService.createOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId",orderNo);
    }

    /**
     * 根据订单id获取订单信息
     * @param orderId
     * @return
     */
    @GetMapping("getOrder/{orderId}")
    public R getOrder(@PathVariable String orderId){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderId);
        Order order = orderService.getOne(queryWrapper);
        return R.ok().data("order",order);
    }

    /**
     * 根据用户id与课程id查询该课程是否被购买
     * @param memberid
     * @param courseid
     * @return
     */
    @GetMapping("isBuyCourse/{memberid}/{courseid}")
    public boolean isBuyCourse(@PathVariable String memberid, @PathVariable String courseid){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id",memberid);
        queryWrapper.eq("course_id",courseid);
        queryWrapper.eq("status",1);
        int count = orderService.count(queryWrapper);
        if (count > 0){
            return true;
        }else{
            return false;
        }
    }

}

